package cc.owoo.godpen.analysis.css;

/**
 * 字符串匹配规则
 * Created by nimensei
 * 2022-06-05 下午 06:38
 */
public interface Examine {
    Examine EQUALS = new Examine() {
        @Override
        public boolean examine(String value, String condition) {
            return value.equals(condition);
        }

        @Override
        public String getCharacter() {
            return "=";
        }
    };// 全等（全部相同）
    Examine LIKE = new Examine() {
        @Override
        public boolean examine(String value, String condition) {
            return value.contains(condition);
        }

        @Override
        public String getCharacter() {
            return "*=";
        }
    };// 模糊包含（任意字串相同）
    Examine WORD = new Examine() {
        @Override
        public boolean examine(String value, String condition) {
            int index = 0;
            while (true) {
                if (value.length() - index < condition.length())
                    return false;
                con:
                {
                    for (int i = 0; i < condition.length(); i++) {
                        char c = value.charAt(index++);
                        if (c <= 32)
                            break con;
                        if (c != condition.charAt(i))
                            break con;
                    }
                    if (index >= value.length() || value.charAt(index) <= 32)
                        return true;
                }
                while (index < value.length())
                    if (value.charAt(index++) <= 32)
                        break;
                while (index < value.length()) {
                    if (value.charAt(index) > 32)
                        break;
                    ++index;
                }
            }
        }

        @Override
        public String getCharacter() {
            return "~=";
        }
    };// 单词包含（两边都有空格的字串相同）
    Examine START = new Examine() {
        @Override
        public boolean examine(String value, String condition) {
            return value.startsWith(condition);
        }

        @Override
        public String getCharacter() {
            return "^=";
        }
    };// 前缀匹配
    Examine END = new Examine() {
        @Override
        public boolean examine(String value, String condition) {
            return value.endsWith(condition);
        }

        @Override
        public String getCharacter() {
            return "&=";
        }
    };// 后缀匹配
    Examine START_WORD = new Examine() {
        @Override
        public boolean examine(String value, String condition) {
            return value.length() > condition.length() &&
                    value.charAt(condition.length()) == '-' &&
                    value.startsWith(condition);
        }

        @Override
        public String getCharacter() {
            return "|=";
        }
    };// 开头单词包含，单词后面必须有-

    /**
     * 创建一个属性选择器的匹配规则
     *
     * @param c 属性选择器符号
     * @return 匹配规则处理对象
     */
    static Examine createExamine(char c) {
        return switch (c) {
            case '=' -> EQUALS;
            case '*' -> LIKE;
            case '~' -> WORD;
            case '^' -> START;
            case '&' -> END;
            case '|' -> START_WORD;
            default -> null;
        };
    }

    /**
     * 判断是否符合条件
     *
     * @param value     需要判断的字符
     * @param condition 用户判断的字符
     * @return 是否符合条件
     */
    boolean examine(String value, String condition);

    /**
     * 获取序列化字符
     *
     * @return 序列化字符
     */
    String getCharacter();
}
